package org.jeecg.modules.drive.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.drive.service.DriveCommonInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class DriveCommonInterfaceServiceImpl implements DriveCommonInterfaceService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public JSONObject httpGet(String url, MultiValueMap<String, String> headerMap, JSONObject paramJsonObj) {


//      String url1 = "http://localhost:8080/jeecg-boot/product/product/testthree";
//         String url = "http://localhost:8007/get_image";
//         paramMap.add("data[name]","image");

//      String back = restTemplate.postForObject(url, paramMap, String.class);


        // 设置请求体
        String requestBody = paramJsonObj.toJSONString();

        // 创建HttpEntity对象，设置请求头和请求体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headerMap);

        String data = restTemplate.postForObject(url, requestEntity, String.class);
        // data = restTemplate.getForObject(url, String.class, requestEntity);
        JSONObject jsonObject = JSONObject.parseObject(data);

        // // 发送POST请求
        // ResponseEntity<String> responseEntity = restTemplate.exchange("http://api.example.com", HttpMethod.POST, requestEntity, String.class);
        // String responseBody = responseEntity.getBody();
        // System.out.println(responseBody);


        //将多余的data去掉 然后进行转换实体类<br>　　　　　　　　　　
        // String suoyou = jsonObject.getString("data");
        // JSONObject jsonObject1 = JSONObject.parseObject(suoyou);

        // Pyback pyback = JSONObject.toJavaObject(jsonObject1, Pyback.class);

        return jsonObject;
    }

    @Override
    public JSONObject httpPost(String url, MultiValueMap<String,String> headerMap, JSONObject paramJsonObj) {
        // 设置请求体
        String requestBody = paramJsonObj.toJSONString();
        // 创建HttpEntity对象，设置请求头和请求体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headerMap);
        String data = restTemplate.postForObject(url, requestEntity, String.class);
        return JSONObject.parseObject(data);
    }
}
